$("#submitLeave").click(function () {
    var data = $("#stuLeave").serialize();
    $.ajax({
        url:"askLeave",
        type:"post",
        data:data,
        success:function () {
            document.querySelector("#stuLeave").reset();
        }
    })
})

layui.use(['layer','jquery','table','form'], function(){
    var layer = layui.layer,
        table = layui.table;
    form = layui.form;
    $ = layui.jquery,
        table.render({
            elem: '#demo'
            ,id: "teacher"
            ,cellMinWidth: 80
            ,url: 'StuLeaveAll' //数据接口
            ,cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'stuname', title: '姓名', width:150}
                    ,{field: 'leavedate', title: '日期', width:150}
                    ,{field: 'state', title: '状态', width:150,templet:function (data) {
                        if (data.state==0){
                            return "未审核";
                        } else  if (data.state==1){
                            return "未通过";
                        }
                        if (data.state==2){
                            return "通过";
                        }
                    }}
                    ,{fixed: 'right', title:'操作', toolbar: '#barCar', width:250}]
            ]
            ,page: ['5','10']
            ,limit:5
            ,toolbar: '#toolbarMenu'
            , done: function (res, curr, count) {
                var that = this.elem.next();
                res.data.forEach(function (item, index) {
                    console.log(item)
                    if (item.state == 0) {
                        var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#7FFFD4");
                    } else if (item.state == 1) {
                        var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#FFC0CB");
                    }else if (item.state == 2) {
                        var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("background-color", "#90EE90");
                    }
                });
            }
        });




    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'update'){
            layer.confirm('是否通过申请?', {
                btn: ["是","否"] //按钮
            }, function(){
                var state = 2;
                del(data.id,state);
            }, function () {
                var state = 1;
                del(data.id,state);
                });
        }
    })

    setInterval(function () {
        table.reload('teacher',{
            page:{curr:1}
        })
    },10000)


        function del(stid,state) {
            console.log(stid)
        $.post("StuAudit",{"id":stid,"state":state},function (d) {
            layer.msg(d.msg, {
                offset: '15px'
                ,icon: 1
                ,time: 1000
            }, function(){
            });
        })
            // 刷新table
            table.reload('teacher',{
                page:{curr:1}
            })
    }
});
